package com.zero.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.zero.manager.common.AuthAccess;
import com.zero.manager.common.Result;
import com.zero.manager.entity.User;
import com.zero.manager.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能：用于登录注册的请求验证
 * 作者：zero
 * 日期：2024/9/7 11:04
 */
@RestController
public class WebController {

    @Resource
    private UserService userService;

    @GetMapping("/")
    public Result hello() {
        return Result.success("success");
    }

    @AuthAccess
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String username = user.getUsername();
        String password = user.getPassword();
        //hutools 校验
        if(StrUtil.isBlank(username)|| StrUtil.isBlank(password)){
            Result.error("用户名不能为空");
        }
        user = userService.login(user);
        return Result.success(user);
    }

    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return Result.error("用户名或密码为空");
        }
        userService.register(user);
        return Result.success(user);
    }
}
